package com.wpw.web.work.invoice;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @ClassName: InvoiceUrlTestThread
 * @CreateDate: 2020-06-13 01:42
 * @author: wupengwei
 */
public class InvoiceUrlTestThread {

    static String jsFile = "/Users/wupengwei/IdeaProjects/webtest/src/main/java/com/wpw/web/work/invoice/noinvoice3.txt";
    static Path jsPath = Paths.get(jsFile);
    static BufferedWriter writer;

    static {
        try {
            writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
        List<String> invoiceList = getInvoiceFromFile();
        List<String> errorList = new ArrayList<>();

        List<List<String>> lists = averageAssign(invoiceList, 30);
        int size = 0;
        for (List<String> list : lists) {
            size = size + list.size();
        }

        for (List<String> list : lists) {
            Thread thread = new Thread(() -> {
                int i = 0;
                for (String invoice : list) {
                    i ++;
                    if (i % 100 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                    String s = checkStatus(invoice);
                    if (StringUtils.isNotBlank(s)) {
                        try {
                            synchronized (InvoiceUrlTestThread.class) {
                                writer.write(s);
                                writer.newLine();
                                writer.flush();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
        }
    }

    public static List<String> getInvoiceFromFile() {

        List<String> invoiceNos = new ArrayList<>();


        String filePath = "/Users/wupengwei/IdeaProjects/webtest/src/main/java/com/wpw/web/work/invoice/invoice.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> codes = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = codes.size();
            System.out.println("共" + totalSize + "条数据");

            for (String code : codes) {
                invoiceNos.add(code);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }

        return invoiceNos;

    }

    public static String checkStatus(String invoiceNo) {
        Map<String, Object> param = new HashMap<>();
        String url = "http://edianzu-pms.oss-cn-beijing.aliyuncs.com/invoice/history/" + invoiceNo + ".jpg";
        try {
            URL u = new URL(url);
            HttpURLConnection uConnection = (HttpURLConnection) u.openConnection();
            uConnection.setReadTimeout(4000);
            uConnection.setConnectTimeout(4000);
            try {
                uConnection.connect();
                int responseCode = uConnection.getResponseCode();
                if (responseCode == 404) {
                    uConnection.disconnect();
                    return invoiceNo;
                }
                uConnection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                //return invoiceNo;
            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return null;
    }

    public static <T> List<List<T>> averageAssign(List<T> source,int n){
        List<List<T>> result=new ArrayList<List<T>>();
        int remaider=source.size()%n; //(先计算出余数)
        int number=source.size()/n; //然后是商
        int offset=0;//偏移量
        for(int i=0;i<n;i++){
            List<T> value=null;
            if(remaider>0){
                value=source.subList(i*number+offset, (i+1)*number+offset+1);
                remaider--;
                offset++;
            }else{
                value=source.subList(i*number+offset, (i+1)*number+offset);
            }
            result.add(value);
        }
        return result;
    }
}